# -*- coding: utf-8 -*-

'''eof
name:借款人当前被列入黑名单
code:R20_PersonalBlackListCheck_WHB
tableName:
columnName:
groups:弱规则>黑名单信息校验
dependencies:WDXT_Black
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import pandas as pd
import json
from datetime import datetime

reload(sys)
sys.setdefaultencoding('utf-8')
null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def checkBlack(base, baseName, blackList):
    if base in null_type_list:
        return "获取到的数据为空"
    try:
        code = base["sysHead"]["responseCode"]
    except:
        return "接口获取失败"
    if code == "0":
        try:
            blackInfo = base.get("body").get(baseName)
        except:
            return "接口获取失败"
        if blackInfo not in null_type_list and isinstance(blackInfo, list):
            if len(blackInfo) >= 1:
                flag = False
                for black in blackInfo:
                    certId = black.get("CERTID")
                    if certId not in null_type_list and certId in blackList:
                        return 0
                    elif certId in null_type_list:
                        flag = True
                if flag:
                    return "接口数据异常"
                return 1
            else:
                return 1
        else:
            return "接口数据异常"
    else:
        return "接口获取失败"


def getRule20():
    certIdList = []
    legalPersonIdCard = ratingRequest.get("legalPersonIdCard")
    if legalPersonIdCard not in null_type_list:
        certIdList.append(legalPersonIdCard)
    else:
        raise RuntimeError("身份证号为空，无法判断个人黑名单")
    blackResult = checkBlack(WDXT_Black, "BLACKLISTDATAS", certIdList)
    if blackResult == 0:
        return 0
    elif blackResult != 1:
        raise RuntimeError(blackResult)
    else:
        return 1


if __name__ == "__main__":
    WDXT_Black = {"sysHead": {"responseCode": "0"}, "body": {"BLACKLISTDATAS": [
        {"CERTTYPE": "Ent02", "CERTID": "510102196706108511"}
    ]}}
    WDXT_AmlBlack = {"sysHead": {"responseCode": "99"}, "body": {"AMLBLACKLISTDATAS": [
        {"CERTTYPE": "Ent02", "CERTID": "123456"}
    ]}}
    ratingRequest = {"legalPerson": u"朱为广", "legalPersonIdCard": "510102196706108511"}
    aa = getRule20()
    print aa

result = getRule20()
